# OA系统登录流程自动化测试脚本
Write-Host "开始测试OA系统登录流程..." -ForegroundColor Green

# 测试配置
$baseUrl = "http://localhost:8000"
$loginEndpoint = "/api/auth/login"
$username = "liuhui"
$password = "zhilv2023"

Write-Host "测试配置信息:" -ForegroundColor Yellow
Write-Host "  基础URL: $baseUrl" -ForegroundColor Yellow
Write-Host "  登录端点: $loginEndpoint" -ForegroundColor Yellow
Write-Host "  测试用户名: $username" -ForegroundColor Yellow

# 1. 测试后端API连接
Write-Host "`n步骤1: 测试后端API连接..." -ForegroundColor Cyan

try {
    $loginData = @{
        username = $username
        password = $password
    } | ConvertTo-Json

    $response = Invoke-WebRequest -Uri "$baseUrl$loginEndpoint" -Method POST -Body $loginData -ContentType "application/json" -TimeoutSec 10
    
    if ($response.StatusCode -eq 200) {
        Write-Host "  OK API连接成功" -ForegroundColor Green
        $responseData = $response.Content | ConvertFrom-Json
        
        if ($responseData.code -eq 0) {
            Write-Host "  OK 登录成功，返回码正确" -ForegroundColor Green
            Write-Host "  OK 登录消息: $($responseData.msg)" -ForegroundColor Green
            Write-Host "  OK 获取到Token: $($responseData.data.token.Substring(0, 20))..." -ForegroundColor Green
            
            # 验证返回数据结构
            if ($responseData.data.token -and $responseData.data.userInfo) {
                Write-Host "  OK 返回数据结构完整" -ForegroundColor Green
            } else {
                Write-Host "  XX 返回数据结构不完整" -ForegroundColor Red
            }
        } else {
            Write-Host "  XX 登录失败，错误码: $($responseData.code)，消息: $($responseData.msg)" -ForegroundColor Red
        }
    } else {
        Write-Host "  XX API连接失败，状态码: $($response.StatusCode)" -ForegroundColor Red
    }
} catch {
    Write-Host "  XX API连接异常: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 2. 验证前端构建文件存在
Write-Host "`n步骤2: 验证前端构建文件..." -ForegroundColor Cyan

$distPath = "d:\phpstudy_pro\WWW\oa.laizone.com\migration\frontend\dist"
$indexPath = "$distPath\index.html"
$assetsPath = "$distPath\assets"

if (Test-Path $distPath) {
    Write-Host "  OK 前端构建目录存在" -ForegroundColor Green
} else {
    Write-Host "  XX 前端构建目录不存在: $distPath" -ForegroundColor Red
}

if (Test-Path $indexPath) {
    Write-Host "  OK index.html文件存在" -ForegroundColor Green
} else {
    Write-Host "  XX index.html文件不存在: $indexPath" -ForegroundColor Red
}

if (Test-Path $assetsPath) {
    $assetFiles = Get-ChildItem -Path $assetsPath
    if ($assetFiles.Count -gt 0) {
        Write-Host "  OK Assets目录包含 $($assetFiles.Count) 个文件" -ForegroundColor Green
    } else {
        Write-Host "  XX Assets目录为空" -ForegroundColor Red
    }
} else {
    Write-Host "  XX Assets目录不存在: $assetsPath" -ForegroundColor Red
}

# 3. 检查关键文件内容
Write-Host "`n步骤3: 检查关键文件内容..." -ForegroundColor Cyan

# 检查Login.vue中是否正确实现了成功消息
$loginVuePath = "d:\phpstudy_pro\WWW\oa.laizone.com\migration\frontend\src\views\Login.vue"
if (Test-Path $loginVuePath) {
    $loginContent = Get-Content $loginVuePath -Raw
    if ($loginContent -match "message\.success") {
        Write-Host "  OK Login.vue中使用了message.success" -ForegroundColor Green
    } else {
        Write-Host "  XX Login.vue中未找到message.success" -ForegroundColor Red
    }
    
    if ($loginContent -match "router\.push\('\/'\)" -or $loginContent -match 'router\.push\("\/"\)') {
        Write-Host "  OK Login.vue中包含正确的路由跳转" -ForegroundColor Green
    } else {
        Write-Host "  XX Login.vue中未找到正确的路由跳转" -ForegroundColor Red
    }
} else {
    Write-Host "  XX Login.vue文件不存在: $loginVuePath" -ForegroundColor Red
}

# 检查路由配置
$routerPath = "d:\phpstudy_pro\WWW\oa.laizone.com\migration\frontend\src\router\index.ts"
if (Test-Path $routerPath) {
    $routerContent = Get-Content $routerPath -Raw
    if ($routerContent -match "path:\s*'/'") {
        Write-Host "  OK 路由配置中包含根路径" -ForegroundColor Green
    } else {
        Write-Host "  XX 路由配置中未找到根路径" -ForegroundColor Red
    }
    
    if ($routerContent -match "Dashboard\.vue") {
        Write-Host "  OK 路由配置中包含Dashboard组件" -ForegroundColor Green
    } else {
        Write-Host "  XX 路由配置中未找到Dashboard组件" -ForegroundColor Red
    }
} else {
    Write-Host "  XX 路由配置文件不存在: $routerPath" -ForegroundColor Red
}

# 4. 检查Dashboard页面
Write-Host "`n步骤4: 检查Dashboard页面..." -ForegroundColor Cyan

$dashboardPath = "d:\phpstudy_pro\WWW\oa.laizone.com\migration\frontend\src\views\Dashboard.vue"
if (Test-Path $dashboardPath) {
    Write-Host "  OK Dashboard.vue文件存在" -ForegroundColor Green
    
    $dashboardContent = Get-Content $dashboardPath -Raw
    if ($dashboardContent -match "dashboard") {
        Write-Host "  OK Dashboard.vue包含预期内容" -ForegroundColor Green
    } else {
        Write-Host "  XX Dashboard.vue内容可能不正确" -ForegroundColor Red
    }
} else {
    Write-Host "  XX Dashboard.vue文件不存在: $dashboardPath" -ForegroundColor Red
}

# 5. 总结
Write-Host "`n测试总结:" -ForegroundColor Cyan
Write-Host "  1. 后端API连接测试: 已完成" -ForegroundColor White
Write-Host "  2. 前端构建文件验证: 已完成" -ForegroundColor White
Write-Host "  3. 关键文件内容检查: 已完成" -ForegroundColor White
Write-Host "  4. Dashboard页面检查: 已完成" -ForegroundColor White

Write-Host "`n请执行以下操作来完成验证:" -ForegroundColor Yellow
Write-Host "  1. 确保后端服务在端口8000运行" -ForegroundColor Yellow
Write-Host "  2. 确保Nginx已配置好API代理" -ForegroundColor Yellow
Write-Host "  3. 重新构建前端应用: npm run build" -ForegroundColor Yellow
Write-Host "  4. 在浏览器中访问前端应用并测试登录功能" -ForegroundColor Yellow
Write-Host "  5. 使用用户名 'liuhui' 和密码 'zhilv2023' 登录" -ForegroundColor Yellow

Write-Host "`n测试完成!" -ForegroundColor Green